package com.yihu.wlyy.service.app.health;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.yihu.wlyy.entity.doctor.Doctor;
import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.entity.patient.PatientHealthGuidance;
import com.yihu.wlyy.repository.DoctorDao;
import com.yihu.wlyy.repository.PatientDao;
import com.yihu.wlyy.repository.PatientHealthGuidanceDao;
import com.yihu.wlyy.service.BaseService;
import com.yihu.wlyy.task.PushMsgTask;
import com.yihu.wlyy.util.DateUtil;

/**
 * 健康指导业务处理类
 * @author George
 *
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class PatientHealthGuidanceService extends BaseService {

	@Autowired
	private DoctorDao doctorDao;
	@Autowired
	private PatientDao patientDao;
	@Autowired
	private PatientHealthGuidanceDao patientHealthGuidanceDao;

	/**
	 * 保存健康指导
	 * @param guidance
	 * @return
	 */
	public PatientHealthGuidance add(PatientHealthGuidance guidance, String accessToken) {
		Doctor doctor = doctorDao.findByCode(guidance.getDoctor());
		Patient patient = patientDao.findByCode(guidance.getPatient());

		guidance.setCzrq(new Date());
		guidance.setDel("1");
		guidance = patientHealthGuidanceDao.save(guidance);
		if (guidance != null && StringUtils.isNotEmpty(patient.getOpenid())) {
			// 推送消息到微信端
			JSONObject json = new JSONObject();
			json.put("first", "健康指导提醒");
			json.put("date", DateUtil.getStringDate());
			json.put("orgName", doctor.getHosptialName());
			json.put("doctorName", doctor.getName());
			json.put("content", "您好！医生给您发来了一份新的健康指导。");
			json.put("remark", "请点击查看");
			PushMsgTask.getInstance().putWxMsg(accessToken, 4, patient.getOpenid(), patient.getName(), json);
		}
		return guidance;
	}

	/**
	 * 查询医生为患者添加的健康指导
	 * @param doctor 医生标识
	 * @param patient 患者标识
	 * @param id
	 * @param pagesize 分页大小
	 * @return
	 */
	public JSONArray findPatientGuidanceByDoctor(String doctor, String patient, String beginStr, String endStr, long id, int pagesize) {
		Date begin = DateUtil.strToDateLong(beginStr);
		Date end = DateUtil.strToDateLong(endStr);
		JSONArray array = new JSONArray();
		Page<Object> page = null;
		PageRequest pageRequest = new PageRequest(0, pagesize);
		if (id > 0) {
			if (StringUtils.isNotEmpty(patient)) {
				if (begin != null && end != null) {
					page = patientHealthGuidanceDao.findByDoctor(doctor, patient, begin, end, id, pageRequest);
				} else {
					page = patientHealthGuidanceDao.findByDoctor(doctor, patient, id, pageRequest);
				}
			} else {
				if (begin != null && end != null) {
					page = patientHealthGuidanceDao.findByDoctor(doctor, begin, end, id, pageRequest);
				} else {
					page = patientHealthGuidanceDao.findByDoctor(doctor, id, pageRequest);
				}
			}
		} else {
			if (StringUtils.isNotEmpty(patient)) {
				if (begin != null && end != null) {
					page = patientHealthGuidanceDao.findByDoctor(doctor, patient, begin, end, pageRequest);
				} else {
					page = patientHealthGuidanceDao.findByDoctor(doctor, patient, pageRequest);
				}
			} else {
				if (begin != null && end != null) {
					page = patientHealthGuidanceDao.findByDoctor(doctor, begin, end, pageRequest);
				} else {
					page = patientHealthGuidanceDao.findByDoctor(doctor, pageRequest);
				}
			}
		}
		if (page != null) {
			for (Object obj : page) {
				Object[] temp = (Object[]) obj;
				if (temp == null || temp.length != 5) {
					continue;
				}
				JSONObject json = new JSONObject();
				json.put("name", temp[0]);
				json.put("photo", temp[1]);
				json.put("id", temp[2]);
				json.put("content", temp[3]);
				json.put("czrq", DateUtil.dateToStrLong((Date) temp[4]));
				array.put(json);
			}
		}
		return array;
	}

	/**
	 * 查询患者的健康指导
	 * @param patient 患者标识
	 * @param id
	 * @param pagesize 分页大小
	 * @return
	 */
	public JSONArray findGuidanceByPatient(String patient, long id, int pagesize) {
		JSONArray array = new JSONArray();
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize);
		Page<Object> page = null;
		if (id > 0) {
			page = patientHealthGuidanceDao.findByPatient(patient, id, pageRequest);
		} else {
			page = patientHealthGuidanceDao.findByPatient(patient, pageRequest);
		}
		if (page != null) {
			for (Object obj : page) {
				Object[] temp = (Object[]) obj;
				if (temp == null || temp.length != 6) {
					continue;
				}
				JSONObject json = new JSONObject();
				json.put("name", temp[0]);
				json.put("photo", temp[1]);
				json.put("id", temp[2]);
				json.put("content", temp[3]);
				json.put("czrq", DateUtil.dateToStrLong((Date) temp[4]));
				json.put("level", temp[5]);
				array.put(json);
			}
		}
		return array;
	}

	public Integer findGuidanceByPatient(String patientCode, String doctor) {
		return patientHealthGuidanceDao.findbydoctorAndPatient(doctor,patientCode);
	}
}
